package cn.com.zhaoweiping.ie;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.converters.Converter;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * <br>
 *
 * @author 冰羽
 * @version 2.2.0
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ExcelField implements Serializable {

  /** 表头标题 */
  private String title;

  /** Java属性字段名 */
  private String name;

  /** Java类型 */
  private Class<?> javaType;

  /** 顺序 */
  private int cellIndex = 0;

  /** 字段值转换器 */
  private Converter converter;

  /** 是否必须 */
  private boolean required = false;

  /** 原始字段信息 */
  private Field original;

  /** 所属实体类型 */
  private Class<?> entityType;

  public String getName() {
    if (StrUtil.isEmpty(name) && original != null) {
      return original.getName();
    }

    return name;
  }

  public Class<?> getJavaType() {
    if (javaType == null && original != null) {
      return original.getType();
    }

    return javaType;
  }

  /** 如果cell value为枚举类型, 则提供下拉选择的方式去填充单元格, key: 列标(order) */
  public Map<Integer, String[]> dropDownValues;

  /**
   * 表头/单元格注释<br>
   *
   * @author 冰羽
   * @version 2.2.0
   */
  @Data
  public static class CommentVo {

    /** 定位行 */
    private int rowIndex;
    /** 定位列 */
    private int colIndex;
    /** 行偏移 */
    private int rowOffset = 1;
    /** 列偏移 */
    private int colOffset = 1;
    /** 注释文本 */
    private String text;
    /** 作者 */
    private String author;

    public CommentVo(int rowIndex, int colIndex, String text) {
      this.rowIndex = rowIndex;
      this.colIndex = colIndex;
      this.text = text;
    }

    public CommentVo(int rowIndex, int colIndex, String text, String author) {
      this(rowIndex, colIndex, text);
      this.author = author;
    }

    public CommentVo(int rowIndex, int colIndex, int rowOffset, int colOffset, String text) {
      this(rowIndex, colIndex, text);
      this.rowOffset = rowOffset;
      this.colOffset = colOffset;
    }

    public CommentVo(
        int rowIndex, int colIndex, int rowOffset, int colOffset, String text, String author) {
      this(rowIndex, colIndex, rowOffset, colOffset, text);
      this.author = author;
    }
  }
}
